package com.xiaomei.manager.web.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;




import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiaomei.manager.model.Order;
import com.xiaomei.manager.model.User;
import com.xiaomei.manager.service.IOrderService;
import com.xiaomei.manager.service.IUserService;
import com.xiaomei.manager.utils.CodeUtils;
import com.xiaomei.manager.utils.OrderPriceUtil;
import com.xiaomei.manager.web.dto.OrderDto;
import com.xiaomei.manager.web.dto.req.OrderQueryDto;
import com.xiaomei.manager.web.page.Page;



@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private IOrderService orderService;
	@Autowired
	private IUserService userService;
	/**
	 * 坐标集合
	 */
	private static List<String> routeLst = new ArrayList<String>();
	private static List<String> routeLst2 = new ArrayList<String>();
	private Logger logger = Logger.getLogger(OrderController.class);
	
	static {
		initRouteLst();
		initRouteLst2();
	}
	@RequiresPermissions("order:view")
    @RequestMapping()
	public String list(HttpServletRequest request, OrderQueryDto dto, Page<OrderDto> page) {
		// 获取订单列表
		if(page.getPageSize()>50) {
			page.setPageSize(50);
		}
		orderService.getOrderForList(dto, page);
		if(page.getTotalRecord()>50) {
			page.setTotalRecord(50);
		}
		request.setAttribute("dto", dto);
		request.setAttribute("page", page);
		return "/order/order";
    }
	@RequiresPermissions("order:create")
	@RequestMapping( value = "/create",method = RequestMethod.GET)
	public String initAddForm(Model model) {
		model.addAttribute("order", new Order());
        model.addAttribute("op", "新增");
        return "order/add";
	}
	
	@RequiresPermissions("order:create")
	@RequestMapping( value = "/create",method = RequestMethod.POST)
	public String addForm(Order order, @RequestParam("files") MultipartFile[] files, String userCode_ck, RedirectAttributes redirectAttributes){
		//StringBuffer sb = new StringBuffer();
		/**
		 * 获取虚拟用户
		 */
		Random random = new Random();
		List<User> userLst = userService.selectFictitiousUser();
		Order order1=new Order();
		order1.setOrderCode(CodeUtils.generateOrderCode());
		//order.setKilometreNo(Double.valueOf(order.getKilometreNo()));
		//order.setPrice(OrderPriceUtil.computeOrderPrice(Double.valueOf(order.getKilometreNo())));
		order1.setVersion(0);
		order1.setState(1);
		order1.setBespeakTimeDesc(order.getBespeakTime().toString().substring(0, order.getBespeakTime().toString().length()-2));
		order1.setBespeakTime(order.getBespeakTime());
		if("0".equals(order.getOrderType())){
			order1.setBaseAddressLabel(order.getBaseAddressLabel());
			get(random, order1);
		}else{
			String Type = null;
			if(order.getOrderType().equals("1")){
				Type="吃饭";	
				get(random, order1);
			}
			if(order.getOrderType().equals("2")){
				Type="看电影";
				get(random, order1);
			}
			if(order.getOrderType().equals("3")){
				Type="唱歌";
				get(random, order1);
			}
			if(order.getOrderType().equals("4")){
				Type="顺风车";
				get(random, order1);
			}
			if(order.getOrderType().equals("5")){
				Type="自驾游";
				get2(random, order1);
			}
			if(order.getOrderType().equals("6")){
				Type="健身";
				get(random, order1);
			}
			if(order.getOrderType().equals("7")){
				Type="其它";	
				get(random, order1);
			}
			order1.setBaseAddressLabel(Type);
			order1.setOrderType(order.getOrderType());
			
		}
		if("1".equals(userCode_ck)) { // 男
			while(true) {
				User user = userLst.get(random.nextInt(userLst.size()));
				if("男".equals(user.getSex())) {
					order1.setPassengerCode(user.getUserCode());
					order1.setVideoShotUrl(user.getImg2());
					order1.setiPickup(order.getiPickup());
					order1.setOfferSits(order.getOfferSits());
					order1.setIsPayOrder(order.getIsPayOrder());
					order1.setPayStyle(order.getPayStyle());
					order1.setIsDriverOrder(1);
					break;
				}
			}
		} else if("2".equals(userCode_ck)) { // 女
			while(true) {
				User user = userLst.get(random.nextInt(userLst.size()));
				if("女".equals(user.getSex())) {
					order1.setPassengerCode(user.getUserCode());
					order1.setVideoShotUrl(user.getImg2());
					order1.setNeedPickup(order.getNeedPickup());
					order1.setNeedSits(order.getNeedSits());
					order1.setPayStyle(order.getPayStyle());
					break;
				}
			}
		}
		
		orderService.insertSelective(order1);
		redirectAttributes.addFlashAttribute("msg", "新增成功");
		return "redirect:/order";
	}
	private void get(Random random, Order order1) {
		int index = random.nextInt(routeLst.size());
		String[] route = routeLst.get(index).split(",");
		order1.setBaseAddressLng(route[0]);
		order1.setBaseAddressLat(route[1]);
		order1.setBaseAddress(route[2]);
		order1.setDestinationLng(route[3]);
		order1.setDestinationLat(route[4]);
		order1.setDestination(route[5]);
		order1.setKilometreNo(Double.valueOf(route[6]));
		order1.setPrice(OrderPriceUtil.computeOrderPrice(Double.valueOf(route[6])));
	}
	private void get2(Random random, Order order1) {
		int index = random.nextInt(routeLst2.size());
		String[] route = routeLst2.get(index).split(",");
		order1.setBaseAddressLng(route[0]);
		order1.setBaseAddressLat(route[1]);
		order1.setBaseAddress(route[2]);
		order1.setDestinationLng(route[3]);
		order1.setDestinationLat(route[4]);
		order1.setDestination(route[5]);
		order1.setKilometreNo(Double.valueOf(route[6]));
		order1.setPrice(OrderPriceUtil.computeOrderPrice(Double.valueOf(route[6])));
	}
	@RequiresPermissions("order:detail")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String detail(@PathVariable("id") int id,HttpServletRequest request){
		Order selectByPrimaryKey = (Order)orderService.selectByPrimaryKey(id);
		request.setAttribute("selectByPrimaryKey", selectByPrimaryKey);
		return "/order/OrderDetails";
	}
	@RequiresPermissions("order:detail")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(Order Order,HttpServletRequest request){
		orderService.updateByPrimaryKeySelective(Order);
		return "redirect:/order";
	}
	
	/**
	 * 初始化坐标信息
	 */
	private static void initRouteLst() {
		routeLst.add("112.997546,28.200551,五一大道,112.919875,28.21207,汽车西站,8.5");
		routeLst.add("112.915539,28.224626,玉兰路西,112.987119,28.195056,柑子园西,9.7");
		routeLst.add("112.95738,28.169301,天马小区,112.97862,28.197332,Mendels island私人会所,7.2");
		routeLst.add("112.979707,28.197667,解放西路湘茶大厦一楼,112.950336,28.185612,湖南大学,6.2");
		routeLst.add("112.950336,28.185612,湖南大学,112.964311,28.209652,望月湖,3.8");
		routeLst.add("112.950336,28.185612,湖南大学,112.949477,28.218474,西湖公园北,5.5");
		routeLst.add("112.949477,28.218474,西湖公园北,112.95738,28.169301,天马小区,7.1");
		routeLst.add("112.949477,28.218474,西湖公园北,112.903643,28.193663,梅溪湖公园,4.4");
		routeLst.add("112.979707,28.197667,解放西路湘茶大厦一楼,112.903643,28.193663,梅溪湖公园,10.2");
		routeLst.add("112.938789,28.170338,中南大学,112.979707,28.197667,解放西路湘茶大厦一楼,9.3");
		routeLst.add("112.982974,28.194936,黄兴广场,112.989493,28.209551,营盘路/司马里(路口),3.3");
		routeLst.add("112.990357,28.241574,北辰三角洲,112.982974,28.194936,黄兴广场,7.7");
		routeLst.add("112.990357,28.241574,北辰三角洲,112.979707,28.197667,解放西,7.5");
		routeLst.add("112.982032,28.195458,司门口,112.991585,28.195438,定王台,2.7");
		routeLst.add("112.982032,28.195458,司门口,112.915539,28.224626,玉兰路西,9.3");
		routeLst.add("112.915539,28.224626,玉兰路西,112.982974,28.194936,黄兴广场,10.3");
		routeLst.add("112.98379,28.205961,长沙市开福区黄兴北路1号金满地商业街,112.982032,28.195458,司门口,3.1");
		routeLst.add("112.982032,28.195458,司门口,112.978007,28.206046,长沙万达文华大酒店,2.7");
		routeLst.add("112.978007,28.206046,长沙万达文华大酒店,112.947505,28.205756,谭石路95号枫林小区,4.9");
		routeLst.add("112.962362,28.204604,江岸景苑,112.98059,28.197502,长沙市天心区魅力四射酒吧,4.6");
		routeLst.add("112.956569,28.191171,湖南师范大学,112.98059,28.197502,长沙市天心区魅力四射酒吧,5.8");
		routeLst.add("112.956569,28.191171,湖南师范大学,112.919875,28.21207,汽车西站,7.8");
		routeLst.add("112.919875,28.21207,汽车西站,112.917378,28.144238,湖南工业职业技术学院,11.7");
		routeLst.add("112.98059,28.197502,长沙市天心区魅力四射酒吧,112.917378,28.144238,湖南工业职业技术学院,13.1");
		routeLst.add("112.917378,28.144238,湖南工业职业技术学院,112.982974,28.194936,黄兴广场,12.5");
		routeLst.add("112.982974,28.194936,黄兴广场,113.032783,28.213579,龙柏小区,6.9");
		routeLst.add("112.984896,28.219082,文昌阁,112.982974,28.194936,黄兴广场,4.5");
		routeLst.add("112.97913,28.197325,菲芘酒吧,112.983377,28.186623,佳兴公寓,3.1");
		routeLst.add("112.987993,28.204056,长沙市房地产大厦,112.979785,28.192032,长郡花园,3.0");
		routeLst.add("112.977183,28.204996,万达影城长沙开福万达广场店,112.979785,28.192032,长郡花园,2.3");
		routeLst.add("112.978007,28.206046,长沙万达文华大酒店,112.917378,28.144238,湖南工业职业技术学院,14.0");
		routeLst.add("112.978007,28.206046,长沙万达文华大酒店,112.882167,28.206717,湖南涉外经济学院,11.8");
		routeLst.add("112.882167,28.206717,湖南涉外经济学院,112.938789,28.170338,中南大学,10.5");
		routeLst.add("112.982032,28.195458,司门口,112.938789,28.170338,中南大学,9.7");
		routeLst.add("112.938789,28.170338,中南大学,112.999764,28.214581,烈士公园,15.2");
		routeLst.add("112.999764,28.214581,烈士公园,112.982974,28.194936,黄兴广场,6.1");
		routeLst.add("112.94378,28.208039,红叶山庄,112.948644,28.22918,科技公寓,4.4");
		routeLst.add("112.94378,28.208039,红叶山庄,112.943979,28.216928,观湖公寓,2.9");
		routeLst.add("112.932003,28.210647,湖南财政经济学院,112.98059,28.197502,长沙市天心区魅力四射酒吧,6.7");
		routeLst.add("112.932003,28.210647,湖南财政经济学院,112.977183,28.204996,万达影城长沙开福万达广场店,5.7");
		routeLst.add("112.932003,28.210647,湖南财政经济学院,112.982974,28.194936,黄兴广场,6.7");
		routeLst.add("112.919875,28.21207,汽车西站,113.007407,28.099931,汽车南站(过渡站),21.3");
		routeLst.add("112.999764,28.214581,烈士公园,112.948644,28.22918,科技公寓,9.3");
		routeLst.add("113.000399,28.217641,湖南省博物馆,113.019647,28.235539,才子佳苑,2.9");
		routeLst.add("113.001459,28.238673,佳阳悦景馨都,112.963699,28.225131,创远花园,7.3");
		routeLst.add("112.960436,28.215334,王陵公园-南门,112.956548,28.203959,湖南指南针商业广场,4.3");
		routeLst.add("112.943979,28.216928,观湖公寓,112.950336,28.185612,湖南大学,6.3");
		routeLst.add("112.929255,28.176087,中南大学-西门,112.95738,28.169301,天马小区,4.4");
		routeLst.add("112.915419,28.156152,南方职院,112.950336,28.185612,湖南大学,6.5");
		routeLst.add("112.930561,28.281994,新地·东方明珠,113.020093,28.24465,左岸春天,14.4");
		routeLst.add("113.058791,28.240531,世界之窗,112.983022,28.194034,黄兴广场,11.9");
		routeLst.add("113.003076,28.150296,科普微公园,112.933327,28.150606,大江南艺术学校,8.4");
		routeLst.add("112.916086,28.216473,佳和园,112.955014,28.195853,湖南传媒产业研究中心,6.8");
		routeLst.add("112.932896,28.212641,西汉长沙王后渔阳墓,112.943556,28.173212,湖南艺术职业学院,8.2");
		routeLst.add("112.906911,28.189394,万科里梅溪郡,113.004655,28.185469,长沙市妇幼保健院,13.8");
		routeLst.add("112.903319,28.219242,麓谷公园,112.986445,28.248679,长沙博物馆新馆,11.5");
		routeLst.add("112.915929,28.28194,谷山体育公园,112.982373,28.283667,北湾景园,9.1");
		routeLst.add("112.925821,28.226873,湖南商学院,112.98059,28.197502,长沙市天心区魅力四射酒吧,9.6");
		routeLst.add("112.983894,28.202046,五一广场,113.019529,28.200066,长沙火车站,3.6");
		routeLst.add("112.890937,28.259025,尖山村北,112.919875,28.21207,汽车西站,5.9");
		routeLst.add("112.87779,28.210724,潇湘大乐影城(涉外西门店),112.892914,28.24197,永安安置小区,6.4");
		routeLst.add("112.990715,28.200609,芙蓉广场,112.96715,28.175466,橘子洲头,5.2");
		routeLst.add("112.976365,28.205111,湘江中路,112.989062,28.180809,贺龙体育馆,4.1");
		routeLst.add("112.950336,28.185612,湖南大学,113.034634,28.211516,马王堆汉墓,12.2");
		routeLst.add("113.034634,28.211516,汽车东站,112.914803,28.215144,湘仪路口,16.0");
		routeLst.add("112.999764,28.214581,烈士公园,112.890937,28.259025,尖山村北,16.4");
		routeLst.add("112.892914,28.24197,永安安置小区,113.08869,28.238135,长沙星沙,26.4");
		routeLst.add("113.058791,28.240531,世界之窗,112.919875,28.21207,汽车西站,17.2");
		routeLst.add("112.903643,28.193663,梅溪湖公园,112.882167,28.206717,湖南涉外经济学院,6.7");
		routeLst.add("112.903319,28.219242,麓谷公园,113.019529,28.200066,长沙火车站,14.4");
		routeLst.add("112.943373,28.194669,岳麓山风景名胜区,112.908102,28.239598,青山镇安置小区,10.0");
		routeLst.add("112.950336,28.185612,湖南大学,112.983894,28.202046,五一广场,6.2");
		routeLst.add("112.975504,28.196635,坡子街,112.941499,28.209562,西湖公园,4.0");
		routeLst.add("112.986445,28.248679,长沙简牍博物馆,113.11741,28.256355,长沙星沙文化中心,20.5");
		routeLst.add("113.048097,28.301193,长沙园林生态园,112.993899,28.195739,芙蓉中路/解放西路(路口),22.1");
		routeLst.add("112.972712,28.147327,南郊公园,112.935037,28.212296,金星路,11.8");
		routeLst.add("112.960627,28.233054,星蓝湾,112.962588,28.207206,岳麓现代城,4.2");
		routeLst.add("112.941499,28.209562,西湖公园,112.982603,28.20616,中山亭,6.7");
		routeLst.add("112.937235,28.212316,龙王港,112.950336,28.185612,湖南大学,7.4");
		routeLst.add("112.958617,28.217739,望月公园,112.985739,28.209642,赐闲湖历史遗迹,5.9");
	}
	private static void initRouteLst2() {
		routeLst2.add("112.9819321,	28.1861978,	黄兴南路75,	111.6109530,	29.8256630,	澧县站	,282.63");
		routeLst2.add("112.9819322,	28.1753560,	轻苑小区,	114.1738300,	27.4745480,	武功山国家级风景名胜区,	185.91");
		routeLst2.add("112.9819323,	28.1749320,	轻苑小区,	112.6935040,27.2646480,	南岳衡山风景名胜区-西门,	142.25");
		routeLst2.add("112.9819324,	28.1753550,	轻苑小区,	110.4592440,	29.3369080,	张家界国家森林公园,	351.39	");
		routeLst2.add("112.9819325,	28.1753550,	轻苑小区,	112.5008210,	27.9143150,	毛泽东故居,	72.18");
		routeLst2.add("112.9819326,	28.1753550,	轻苑小区,	112.6535820,	28.0382580,	刘少奇同志纪念馆,	44.27");
		routeLst2.add("112.9819327,	28.1753550,	轻苑小区,	109.6115900,	27.9554600,	凤凰古城步行街,	442.78");
		routeLst2.add("112.9819328,	28.1753550,	轻苑小区,	113.0215300,	29.1263360,	洞庭湖,	147.31");
		routeLst2.add("112.9819329,	28.1753550,	轻苑小区,	110.4661330,29.3543560,	武陵源风景名胜区,	350.95");
		routeLst2.add("112.9819330,	28.1753550,	轻苑小区,	112.8008450,	28.4670180,	靖港古镇(东门),	43.73");
		routeLst2.add("112.9819331,	28.1749460,	轻苑小区,	111.7659160,	29.0697120,	柳叶湖旅游度假区,	186.34");
		routeLst2.add("112.9861920,	28.1749460,	轻苑小区,	109.9814040,	28.7730170,	芙蓉镇,	414.81");
		routeLst2.add("112.9861300,	28.1755190,	轻苑小区,	114.0899470,	28.4281990,	大围山国家森林公园,	140.38");
		routeLst2.add("112.9861300,	28.1755190,	轻苑小区,	110.0124130,	27.1209470,	洪江古商城,	391.35");
		routeLst2.add("112.9861300,	28.1755190,	轻苑小区,	112.4663440,	27.4146240,	曾国藩故居,	132.32");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	109.5982400,	28.3472040,	德夯苗寨,	432.46");
		routeLst2.add("112.9861460,	28.1753150,	轻院路141,	109.2823150,	28.5089160,	边城茶峒,	465.37");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	111.9919910,	25.3568520,	九嶷山,	405.17");
		routeLst2.add("112.9861460,	28.1753150,	轻院路141,	109.9433660,	27.4286590,	荆坪古村,	402.49");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	111.4955680,	28.9087420,	桃源县政府,	199.10");
		routeLst2.add("112.9861460,	28.1753150,	轻院路141,	112.8167220,	28.4811200,	铜官镇,	42.62");
		routeLst2.add("112.9861460,	28.1753150,	轻院路141	,113.6746700,	28.4356510,	周洛大峡谷,	109.97");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	111.1565420,	27.2648490,	滩头镇,	248.08");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	109.7909340,	26.1641660,	通道侗族自治县,	564.23");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	109.3029710,	28.7887650,	里耶,	499.22");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	112.1557050,	28.5181850,	桃江,	110.95");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	112.8727220,	26.0842990,	板梁村,	262.28");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	120.0304500,	31.8595490,	吴王七太子墓,	1019.29");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	113.7312870,	28.4743150,	白寺村,	121.66");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	109.5402320,	29.0721090,	洗车河镇,	468.71");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	112.9189970,	27.8614880,	寿佛殿社区,	43.73");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	112.5388840,	27.7419750,	云门寺,	91.11");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	111.6573160,	28.1434650,	梅城镇,	193.69");
		routeLst2.add("112.9861460,	28.1753150,	轻苑小区,	113.2942820,	26.6942770,	稻田公园,	221.48");
		
	}		

}
